package com.ccys.cms.service.userManagement.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.userManagement.TemplateService;
import com.ccys.common.util.Kit;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.userManagement.TemplateDto;
import com.ccys.db.entity.TblTemplate;
import com.ccys.db.mapper.TblTemplateMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.userManagement.TemplatePageQueryVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: 模板管理接口实现类
 * @author: ZhangJingCan
 * @create: 2023-12-20 13:32
 **/

@Service
public class TemplateServiceImpl extends BaseServiceImpl<TblTemplate> implements TemplateService {

    @Resource
    private TblTemplateMapper tblTemplateMapper;

    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveTemplate(TemplateDto dto) {
        //校验字段
        Kit.allParamValid(dto, "name", "groupId", "dispOrder", "template");

        Example example = new Example(TblTemplate.class);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("groupId", dto.getGroupId()).andEqualTo("name", dto.getName()).andEqualTo("delFlag", 0);
        //如果是修改 - 需要排除自身
        if (ObjectUtil.isNotEmpty(dto.getId())) {
            //排除自身
            criteria.andNotEqualTo("id",dto.getId());
        }
        if (ObjectUtil.isNotEmpty(tblTemplateMapper.selectOneByExample(example))) {
            return ResponseResult.error("This name already exists in the current group!");
        }
        //新增或修改 - 并返回是否成功
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteTemplate(TemplateDto dto) {
        //判断
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备实体类并赋值
        TblTemplate tblTemplate = new TblTemplate();
        tblTemplate.setId(dto.getId());
        tblTemplate.setDelFlag(1);
        //判断是否修改成功 - 返回不同状态
        if (tblTemplateMapper.updateByPrimaryKeySelective(tblTemplate) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateTemplateStatus(TemplateDto dto) {
        //校验
        if (ObjectUtil.isEmpty(dto.getId()) || ObjectUtil.isEmpty(dto.getStatus())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备数据修改
        TblTemplate tblTemplate = new TblTemplate();
        tblTemplate.setId(dto.getId());
        tblTemplate.setStatus(dto.getStatus());
        if (tblTemplateMapper.updateByPrimaryKeySelective(tblTemplate) > 0) {
            //保存状态修改日志
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
            return ResponseResult.success("Modified successfully!");
        }
        return ResponseResult.error("Modification failed!");
    }

    @Override
    public ResponseResult<PageInfo<TemplatePageQueryVo>, Object> findPageTemplate(TemplateDto dto) {
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        //开启分页
        PageDto.startPage();
        List<TemplatePageQueryVo> list = tblTemplateMapper.findPageTemplate(dto);
        PageInfo<TemplatePageQueryVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateDispOrder(TemplateDto dto) {
        //校验字段
        if (ObjectUtil.isEmpty(dto.getId()) || ObjectUtil.isEmpty(dto.getDispOrder())) {
            return ResponseResult.error("illegal request!");
        }
        //查询是否存在这个数据
        Example example = new Example(TblTemplate.class);
        example.createCriteria()
                .andEqualTo("id", dto.getId()).andEqualTo("delFlag", 0);
        TblTemplate tblTemplate = tblTemplateMapper.selectOneByExample(example);
        if (ObjectUtil.isEmpty(tblTemplate)) {
            return ResponseResult.error("illegal request!");
        }
        //以上都没问题，则修改数据
        tblTemplate.setDispOrder(dto.getDispOrder());
        if (tblTemplateMapper.updateByPrimaryKeySelective(tblTemplate) > 0) {
            return ResponseResult.success("Modified successfully!");
        }
        return ResponseResult.error("Modification failed!");
    }
}
